//
//  DBOriginalView.swift
//  大波微博swift
//
//  Created by 大波 on 16/3/4.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit
import SnapKit

class DBOriginalView: UIView {
    
    private let DBStatusesCellMargin: CGFloat = 8.0
    
    private var bottomConstrain: Constraint?
    
    var statuse: DBStatuses? {
        
        didSet {
            
            // 头像设置
            if let iconURLString = statuse?.user?.profile_image_url {
                
                iconView.sd_setImageWithURL(NSURL(string: iconURLString))
            }
            
            // 微博名设置
            nameLabel.text = statuse?.user?.screen_name
            
            // 会员头像设置
            if statuse?.user?.mbrank > 0 && statuse?.user?.mbrank <= 6 {
                
                memberView.image = UIImage(named: "common_icon_membership_level\(statuse?.user?.mbrank)")
            }
            
            // 设置时间
            timeLabel.text = statuse?.created_at
            
            // 设置来源
            sourceLabel.text = statuse?.source
            
            // 设置微博内容
            textLabel.text = statuse?.text
            
            // 认证图标 没有认证:-1   认证用户:0  企业认证:2,3,5  达人:220
            if let verified_type = statuse?.user?.verified_type {
                
                switch verified_type {
                    
                case 0:
                    verifiedTypeImage.image = UIImage(named: "avatar_grassroot")
                    
                case 2, 3, 5:
                    verifiedTypeImage.image = UIImage(named: "avatar_enterprise_vip")
                    
                case 220:
                    verifiedTypeImage.image = UIImage(named: "avatar_vip")
                    
                default:
                    verifiedTypeImage.image = nil
                }
            }
            
            // 去除约束
            bottomConstrain?.uninstall()
            
            if let picture = statuse?.pic_urls where picture.count > 0 {
                
                pictureView.statuse = statuse
                
                pictureView.hidden = false
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    //                    make.bottom.equalTo(pictureView.snp_bottom).offset(DBStatusesCellMargin + 1)
                    
                    
                    self.bottomConstrain = make.bottom.equalTo(pictureView).offset(DBStatusesCellMargin).constraint
                })
                
            } else {
                
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    //                    make.bottom.equalTo(textLabel.snp_bottom).offset(DBStatusesCellMargin)
                    
                    
                    self.bottomConstrain = make.bottom.equalTo(textLabel).offset(DBStatusesCellMargin).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        prepareUI()
    }
    
    private func prepareUI() {
        
        addSubview(iconView)
        
        addSubview(nameLabel)
        
        addSubview(timeLabel)
        
        addSubview(sourceLabel)
        
        addSubview(textLabel)
        
        addSubview(verifiedTypeImage)
        
        addSubview(memberView)
        
        addSubview(pictureView)
        
        addConstraint()
    }
    
    func addConstraint() {
        
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            
            //            make.top.leading.equalTo(self).offset(DBStatusesCellMargin)
            //
            //            make.width.height.equalTo(35)
            
            
            make.leading.equalTo(self).offset(DBStatusesCellMargin)
            make.top.equalTo(self).offset(DBStatusesCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        // 认证图片
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            
            //            make.centerX.equalTo(iconView.snp_trailing)
            //
            //            make.centerY.equalTo(iconView.snp_bottom)
            
            
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        // 微博名
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            //            make.top.equalTo(iconView)
            //
            //            make.leading.equalTo(iconView.snp_trailing).offset(DBStatusesCellMargin)
            
            
            make.leading.equalTo(iconView.snp_trailing).offset(DBStatusesCellMargin)
            make.top.equalTo(iconView)
        }
        
        // 会员
        memberView.snp_makeConstraints { (make) -> Void in
            
            //            make.top.equalTo(nameLabel)
            //
            //            make.leading.equalTo(nameLabel.snp_trailing).offset(DBStatusesCellMargin)
            
            
            make.leading.equalTo(nameLabel.snp_trailing).offset(DBStatusesCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            //            make.bottom.equalTo(iconView)
            //
            //            make.leading.equalTo(iconView.snp_trailing).offset(DBStatusesCellMargin)
            
            
            make.leading.equalTo(iconView.snp_trailing).offset(DBStatusesCellMargin)
            make.bottom.equalTo(iconView)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            //            make.leading.equalTo(timeLabel.snp_trailing).offset(DBStatusesCellMargin)
            //
            //            make.bottom.equalTo(timeLabel)
            
            
            make.leading.equalTo(timeLabel.snp_trailing).offset(DBStatusesCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        // 正文约束
        textLabel.snp_makeConstraints { (make) -> Void in
            
            //            make.top.equalTo(iconView.snp_bottom).offset(DBStatusesCellMargin)
            //
            //            make.leading.equalTo(self.snp_leading).offset(DBStatusesCellMargin)
            //
            //            make.trailing.equalTo(self.snp_trailing).offset(-DBStatusesCellMargin)
            
            
            make.top.equalTo(iconView.snp_bottom).offset(DBStatusesCellMargin)
            make.leading.equalTo(self).offset(DBStatusesCellMargin)
            make.trailing.equalTo(self).offset(-DBStatusesCellMargin)
        }
        
        // 微博配图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            
            //            make.top.equalTo(textLabel.snp_bottom).offset(DBStatusesCellMargin)
            //
            //            make.leading.equalTo(self).offset(DBStatusesCellMargin)
            //
            //            make.size.equalTo(CGSize(width: 200, height: 200))
            
            
            
            make.leading.equalTo(self).offset(DBStatusesCellMargin)
            make.top.equalTo(textLabel.snp_bottom).offset(DBStatusesCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            //            self.bottomConstrain = make.bottom.equalTo(pictureView.snp_bottom).offset(DBStatusesCellMargin + 1).constraint
            
            
            self.bottomConstrain = make.bottom.equalTo(pictureView).offset(DBStatusesCellMargin).constraint
        }
    }
    
    //MARK: - 控件懒加载
    
    /// 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 认证标识
    private lazy var verifiedTypeImage: UIImageView = UIImageView(image: nil)
    
    /// 名字
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), font: 14)
    
    /// 会员图标
    private lazy var memberView: UIImageView = UIImageView(image: nil)
    
    /// 时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), font: 11)
    
    /// 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), font: 11)
    
    /// 正文
    private lazy var textLabel: FFLabel = {
        
        let text = FFLabel(textColor: UIColor.darkGrayColor(), font: 15)
        
        text.numberOfLines = 0
        
        text.labelDelegate = self
        
        return text
    }()
    
    /// 图片View
    private lazy var pictureView: DBPictureView = DBPictureView()
}


/// 打开链接通知名称
let DBLoadLinkNotification = "DBLoadLinkNotification"

/// 通知附带的链接地址 key名
let DBLoadLinkKey = "DBLoadLinkKey"


// MARK: - FFLabelDelegate 代理

extension DBOriginalView: FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        //        print("labelDidSelectedLinkText: \(text)")
        if text.hasPrefix("http") {
            
            NSNotificationCenter.defaultCenter().postNotificationName(DBLoadLinkNotification, object: self, userInfo: [DBLoadLinkKey : text])
        }
    }
}







